// JavaScript Document
//============================================================================================================
//~ Checks to see if a required field is blank.  If so, an error message is displayed.
function ForceDataEntry(objField, strFieldName)
{
	// To find out the Value of the control
	var strFieldValue=trimAll(objField.value);
	if (IsEmpty(strFieldValue))
	{
		alert("You need to enter " + strFieldName);
		objField.focus();
		return false;
	}
	return true;
}
//==============================================================================================================

//~ Checks to see if a DropDownList is empty. If so, an error message is displayed.
function ForceComboItems(ddlName, strDdlName)
{
	// To find out the Value of the control
	var ItemCount=ddlName.value;
	if (IsComboEmpty(ItemCount))
		{
			alert("First Select the " + strDdlName);
			ddlName.focus();
			return false;
		}
	return true;
}
//===============================================================================================================
//================================================================================================================
//~ Check whether a given string is empty or not.
function IsEmpty(strTxtBoxValue)
{   if ((strTxtBoxValue == null) || (strTxtBoxValue.length == 0))
	{
		return true;			//^ Returning True, if Empty Text Box found
	}	
}

//=====================================================================================================
//================================================================================================================
//~ Check whether a given DropDown value is empty or not.

function IsComboEmpty(ddlValue)
{ 
    if( (ddlValue =="--Select--")|| (ddlValue=="0"))
    {
    return true; //^ Returning True, if Empty Text Box found
    } 
}



function CheckKeyCode()

{
if((event.keyCode >= 96 && event.keyCode <= 105) ||

(event.keyCode > 32 && event.keyCode < 48) ||

(event.keyCode > 57 && event.keyCode < 65) ||

(event.keyCode > 90 && event.keyCode < 96) ||

(event.keyCode == 189 || event.keyCode == 109))


{

return true; }

else {

return false;

}

}

function noCopyKey(e) 

{

var forbiddenKeys = new Array('c','x','v');

var isCtrl;

if(window.event) {

if(window.event.ctrlKey)

isCtrl = true;

else

isCtrl = false;

}

else {

if(e.ctrlKey)

isCtrl = true;

else

isCtrl = false; }

if(isCtrl) {

for(i=0; i < forbiddenKeys.length; i++) {

if(forbiddenKeys[i] == String.fromCharCode(window.event.keyCode).toLowerCase()) {

alert('You can not copy and paste here');

return false;

}

}

}

return true;

}


function noCopyMouse(e) 

{

//debugger;

var message="";

if (event.button == 2 || event.button == 3) 

{

//this.focus();

alert('You can not copy and paste here');

return false;

}

return true;

}
	
//===============================================================================================================
//================================================================================================================
//~ Check whether a given Email is Valid or not.
function IsEmailValid(objField, strFieldName)
{
	// To find out the Value of the control
	var strFieldValue=objField.value;
	if (IsEmpty(strFieldValue))
	{
		return true;
	}
	else
	{
	    var at="@"
		var dot="."
		var lat=strFieldValue.indexOf(at)
		var lstr=strFieldValue.length
		var ldot=strFieldValue.indexOf(dot)
		
		if (strFieldValue.indexOf(at)==-1)
		{
		    alert("Email should be in abc@xyz.com format");
		   objField.focus();
		   return false;
		}
		if (strFieldValue.indexOf(at) == -1 || strFieldValue.indexOf(at) == 0 || strFieldValue.indexOf(at) == lstr)
        {
		   alert("Email should be in abc@xyz.com format");
		   objField.focus();
		   return false;
		}
		if (strFieldValue.indexOf(dot)== lstr || strFieldValue.indexOf(dot)== -1 || strFieldValue.indexOf(dot)== 0  )
        {
		   alert("Email should be in abc@xyz.com format");
		   objField.focus();
		   return false
		}
		if ((ldot+1)  == lstr   )
        {
        
		    alert("Email should be in abc@xyz.com format");
		    objField.focus();
		    return false
		}
		 if (strFieldValue.indexOf(at,(lat+1))!= -1)
         {
		    alert("Email should be in abc@xyz.com format");
		    objField.focus();
		    return false;
		 }
		 if (strFieldValue.substring(lat-1,lat) == dot || strFieldValue.substring(lat+1,lat+2)== dot)
         {
		   alert("Email should be in abc@xyz.com format");
		   objField.focus();
		   return false;
		 }

		 if (strFieldValue.indexOf(dot,(lat+2))== -1)
         {
		    alert("Email should be in abc@xyz.com format");
		    objField.focus();
		    return false;
		 }
		 
		 if (lstr == strFieldValue.indexOf(dot)+2 || lstr == strFieldValue.indexOf(dot)+5)
          {
		       alert("Email should be in abc@xyz.com format");
		       objField.focus();
		       return false;
		       
		      }
		 if (strFieldValue.indexOf(" ")!= -1)
         {
		     alert("Email should be in abc@xyz.com format");
		     objField.focus();
		     return false;
		 }
		return true;
		}
	
	
}
//--------------------------------------------------------------------------------------------------------------------
 //===============================================================================================================
 //===============================================================================================================
//~ Returns true if the string passed is a valid number
//~ no characters except a -ve sign at the begining is accepted
//~ otherwise, it displays an error message
function ForceNumericEntry(objField, strFieldName,IsMandatory)
{
	var strFieldValue = new String(objField.value);
    var intLoopCounter = 0;
    
    //^ If mandatory field => testing for blank or spaces
    if (IsMandatory=="T")
    {
        if (IsEmpty(strFieldValue))
        {
            alert(strFieldName + " can not be left blank");
            objField.focus();
            return false;
        }
    }
    else    //^ If non-mandatory field => blank or spaces is ignored
    if (IsMandatory=="F")
    {
        if (IsEmpty(strFieldValue))
        {
            return true;
        }
    }

    // Checking for spaces at the begining of text.
    if (strFieldValue.indexOf(" ")!= -1)
    {
        alert(strFieldName + " must be a valid No.");
        objField.focus();
        return false;
    }

    // Validating for a numeric non-decimal value.
	for (intLoopCounter = 0; intLoopCounter < strFieldValue.length; intLoopCounter++)
	{
		//if ((strFieldValue.charAt(intLoopCounter) < '0' || strFieldValue.charAt(intLoopCounter) > '9') && (strFieldValue.charAt(0) != '-'))
		if  (strFieldValue.charAt(intLoopCounter) == '.')
		{
			alert(strFieldName + " does not accept a decimal value.");
			objField.focus();
			return false;
		}
		if  ((strFieldValue.charAt(intLoopCounter) > '9') && (strFieldValue.charAt(0) != '-'))
		{
			alert(strFieldName + " must be a valid No.");
			objField.focus();
			return false;
		}
	}
		
    //^ Validating for the Valid Numeric value to be > 0		
    var strLength = strFieldValue.length;
    intLoopCounter = 0;
    var ZeroValue = "Y";
    
    while (intLoopCounter < strLength)
    {
            if (strFieldValue.charAt(intLoopCounter) == '0')
            {
                intLoopCounter = intLoopCounter + 1;
            }
            else
            {
                ZeroValue = "N"  
                break ;
            }
    }
    if (ZeroValue == "Y")
    {
		alert(strFieldName + " must be greater than 0");
		objField.focus();
		return false;
    }
    
	return true;
}


function ForceNumericEntryDate(objField, strFieldName)
{
	var strFieldValue = new String(objField.value);
    var intLoopCounter = 0;

	for (intLoopCounter = 0; intLoopCounter < strFieldValue.length; intLoopCounter++)
		//if ((strFieldValue.charAt(intLoopCounter) < '0' || strFieldValue.charAt(intLoopCounter) > '9') && (strFieldValue.charAt(0) != '-'))
		
		if  ((strFieldValue.charAt(intLoopCounter) > '9') && (strFieldValue.charAt(0) != '/'))
		{
			alert(strFieldName + " must be a valid Date, Valid Date Format is : dd/mm/yyyy");
			objField.focus();
			return false;
		}
	return true;
}
//================================================================================

//==========================================================================================================
// Validates a Date Entered by User 
// Also Forces the User to stick to either "dd/MMM/yyyy" or "dd-MMM-yyyy" Date Formats.

	function isValidDate(objField,strDateName)
	{
     var strDateValue=trimAll(objField.value)
    
    // If Date is Blank, Don't Validate
    if (IsEmpty(strDateValue)) return true;
     
       
     var arr = [];
     arr['JAN'] = 0;
     arr['FEB'] = 1;
     arr['MAR'] = 2;
     arr['APR'] = 3;
     arr['MAY'] = 4;
     arr['JUN'] = 5;
     arr['JUL'] = 6;
     arr['AUG'] = 7;
     arr['SEP'] = 8;
     arr['OCT'] = 9;
     arr['NOV'] = 10;
     arr['DEC'] = 11;
     
     //^ Splitting the date using the divider "/"
     var arrDate = strDateValue.split('/');
     
     //^ If user did not use the divider "/" then,
     if (arrDate.length!=3)
     {
        //^ Splitting the date using the divider "-"
         arrDate = strDateValue.split('-');
     }
     
     // if splitting occured using the correct divider
     if (arrDate.length==3 && strDateValue.length==11)
     {
       //^ d stores date entered by user in Javascript Internal Date Format
       var d = new Date(arrDate[2], arr[arrDate[1].toUpperCase()], arrDate[0]);
     
       //Checking if the date entered is a valid Date or not
       if (!isNaN(d) && d.getFullYear()==arrDate[2] && d.getMonth()==arr[arrDate[1].toUpperCase()] && d.getDate()==arrDate[0])
       {
             return true;
       }
       else
       {
            alert(strDateName + " Valid Date Formats Are : DD-MMM-YYYY");
            objField.focus();
            return false;
       }
     }
     else
     {
        alert("Valid Date Formats Are :DD-MMM-YYYY");
        objField.focus();
        return false;
     }
}

//=============================================================================================================
//=============================================================================================================

// Function for trim any string
function trimAll( strValue )
			 {				
				var objRegExp = /^(\s*)$/;
					      //check for all spaces
					if(objRegExp.test(strValue))
					 {
					strValue = strValue.replace(objRegExp, '');
					if( strValue.length == 0)
						return strValue;
					 }
				        //check for leading & trailing spaces
				    objRegExp = /^(\s*)([\W\w]*)(\b\s*$)/;
				    if(objRegExp.test(strValue)) 
				     {
					//remove leading and trailing whitespace characters
					strValue = strValue.replace(objRegExp, '$2');
				     }
				return strValue;
			 }


//==================================
function isDecimalValue(fieldName, fieldLabel)
{
    decallowed = 2;  // how many decimals are allowed?
    fieldValue = fieldName.value;
    
    if (isNaN(fieldValue) || fieldValue == "") 
    {
        alert(fieldLabel + " is not a valid number.");
        fieldName.select();
        fieldName.focus();
        return false;
    }
    else 
    {
        if (fieldValue.indexOf('.') == -1) fieldValue += ".";
            dectext = fieldValue.substring(fieldValue.indexOf('.')+1, fieldValue.length);
    
        if (dectext.length > decallowed)
        {
            alert (fieldLabel + " allows up to " + decallowed + " decimal places only.");
            fieldName.select();
            fieldName.focus();
            return false;
        }
        else 
        {
            return true;
        }
    }
}

// Check to see if input is alphabetic
function ForceAlphabetic(objField, strFieldName)
{
	if (objField.value.match(/^[.a-zA-Z\s]+$/))
	{
		return true;
	}
	else
	{
		alert(strFieldName + " must have only Alphabetic values");
		objField.focus();
		return false;
	} 
}

function roundNumber(num, dec) 
{
	var result = Math.round(num*Math.pow(10,dec))/Math.pow(10,dec);
	return result;
}

function ForceValidDateValue(objField,objValue,strDateName)
{
    //debugger;
     var strDateValue=objValue;
    
    // If Date is Blank, Don't Validate
    if (trimAll(strDateValue).Length <= 2) return true;
     
     //^ Splitting the date using the divider "/"
     var arrDate = strDateValue.split('/');
     
     //^ If user did not use the SEPERATOR "/" then,
     if (arrDate.length!=3)
     {
        alert("Valid Date Format is : dd/mm/yyyy");
        objField.focus();
        return false;
     }
     
     // if splitting occured using the correct SEPERATOR ("/")
     if (strDateValue.length==10)
     {
       //^ d stores date entered by user in Javascript Internal Date Format
       var d = new Date(arrDate[2], arrDate[1]-1, arrDate[0]);
     
       //Checking if the date entered is a valid Date or not
       if (!isNaN(d) && d.getFullYear()==arrDate[2] && d.getMonth()==arrDate[1]-1 && d.getDate()==arrDate[0])
       {
             return true;
       }
       else
       {
	        alert(strDateName + " is an Invalid Date., Valid Date Format is : dd/mm/yyyy");
            objField.focus();
            return false;
       }
     }
     else
     {
		alert("Valid Date Format is : dd/mm/yyyy");
        objField.focus();
        return false;
     }
}


//================================================================================================================================================================

function ValidateContactUs()
{
	//# Validating Company Name for blank
	if (!ForceDataEntry(document.ContactUs.strName1,'name')) return false;
	
	//if (!ForceDataEntry(document.ContactUs.strEmail1, 'Email')) return false;
	//# Validating Email Address for Valid Format
	//if (!IsEmailValid(document.ContactUs.strEmail1,'Enter proper Email')) return false;
	
	if (!ForceDataEntry(document.ContactUs.strPhone,'Phone')) return false;
	if (!ForceNumericEntry(document.ContactUs.strPhone,'Phone No')) return false;
	
}

function ValidateSubscribe()
{
	
	if (!ForceDataEntry(document.Subscriber.strEmail, 'Email')) return false;
	//# Validating Email Address for Valid Format
	if (!IsEmailValid(document.Subscriber.strEmail,'Enter proper Email')) return false;
	
	if (!ForceDataEntry(document.Subscriber.strName,'Name')) return false;
}

function ValidatePartnerApplication()
{
	if (!ForceDataEntry(document.partnerapplication.txtName, 'Name')) return false;
	if (!ForceDataEntry(document.partnerapplication.txtCompany, 'Company Name')) return false;
	if (!ForceDataEntry(document.partnerapplication.txtdesignation, 'Designation')) return false;
	
	if (!ForceDataEntry(document.partnerapplication.txtAge, 'Age')) return false;
	if (!ForceNumericEntry(document.partnerapplication.txtAge,'Age')) return false;
	
	if (!ForceComboItems (document.partnerapplication.Gender,'Gender')) return false;
	if (!ForceNumericEntry(document.partnerapplication.txtPin,'Pin Code')) return false;
	if (trimAll(document.partnerapplication.txtPhoneNo.value) == "" && 
				trimAll(document.partnerapplication.txtMobile.value) == "") 
	{
		alert("Phone No. or Mobile No. is Required");
		document.partnerapplication.txtPhoneNo.focus();
		return false;
	}
	if (!ForceNumericEntry(document.partnerapplication.txtPhoneNo,'Phone No')) return false;
	if (!ForceNumericEntry(document.partnerapplication.txtExtn,'Extension')) return false;
	if (!ForceNumericEntry(document.partnerapplication.txtMobile,'Mobile No')) return false;
	
	if (!ForceDataEntry(document.partnerapplication.txtEmailId, 'Email Id')) return false;
	if (!IsEmailValid(document.partnerapplication.txtEmailId,'Enter proper Email')) return false;
	
	if (!ForceComboItems (document.partnerapplication.Workshop,'Workshop')) return false;
	
	if (!ForceValidDateValue(document.partnerapplication.txtDate1,document.partnerapplication.txtDate1.value,"Date")) return false;
	if (!ForceComboItems (document.partnerapplication.hh1,'Hours')) return false;
	if (!ForceComboItems (document.partnerapplication.mm1,'Minuts')) return false;
	if (!ForceComboItems (document.partnerapplication.ampm1,'AM/PM')) return false;
	
	if (trimAll(document.partnerapplication.txtDate2.value) != "")
	{
		if (!ForceValidDateValue(document.partnerapplication.txtDate2,document.partnerapplication.txtDate2.value,"Date")) return false;
		if (!ForceComboItems (document.partnerapplication.hh2,'Hours')) return false;
		if (!ForceComboItems (document.partnerapplication.mm2,'Minuts')) return false;
		if (!ForceComboItems (document.partnerapplication.ampm2,'AM/PM')) return false;
	}
	
	
	//if (!ForceDataEntry(document.Subscriber.strEmail, 'Email')) return false;
	
	
	//if (!ForceDataEntry(document.Subscriber.strName,'Name')) return false;
}



//----------------------After Merging---------------------//
//================================================================================================================================================================


//==============================================================================================================
function ForceDecimalEntry(objField, strFieldName,IsMandatory)
{
	var strFieldValue = new String(objField.value);
    var intLoopCounter = 0;
    
    //^ If mandatory field => testing for blank or spaces
    if (IsMandatory=="T")
    {
        if (IsEmpty(strFieldValue))
        {
            alert(strFieldName + " can not be left blank");
            objField.focus();
            return false;
        }
    }
    else    //^ If non-mandatory field => blank or spaces is ignored
    if (IsMandatory=="F")
    {
        if (IsEmpty(strFieldValue))
        {
            return true;
        }
    }

    // Checking for spaces at the begining of text.
    if (strFieldValue.indexOf(" ")!= -1)
    {
        alert(strFieldName + " must be a valid No.");
        objField.focus();
        return false;
    }

    intCtr = 0;
    // Validating for a numeric non-decimal value.
	for (intLoopCounter = 0; intLoopCounter < strFieldValue.length; intLoopCounter++)
	{
	   
		//if ((strFieldValue.charAt(intLoopCounter) < '0' || strFieldValue.charAt(intLoopCounter) > '9') && (strFieldValue.charAt(0) != '-'))
		if  (strFieldValue.charAt(intLoopCounter) == '.')
		{
		    if (intCtr > 0)
		    {
			    alert(strFieldName + " does not accept more than one decimal");
			    objField.focus();
			    return false;
		    }
		    intCtr = intCtr + 1;
		}
		if  ((strFieldValue.charAt(intLoopCounter) > '9') && (strFieldValue.charAt(0) != '-'))
		{
			alert(strFieldName + " must be a valid No.");
			objField.focus();
			return false;
		}
	}
		
    //^ Validating for the Valid Numeric value to be > 0		
    var strLength = strFieldValue.length;
    intLoopCounter = 0;
    var ZeroValue = "Y";
    
    while (intLoopCounter < strLength)
    {
            if (strFieldValue.charAt(intLoopCounter) == '0')
            {
                intLoopCounter = intLoopCounter + 1;
            }
            else
            {
                ZeroValue = "N"  
                break ;
            }
    }
    if (ZeroValue == "Y")
    {
		alert(strFieldName + " must be greater than 0");
		objField.focus();
		return false;
    }
    
	return true;
}


//function ValidateContactUs()
//{
//
//	//# Validating Company Name for blank
//	if (!ForceDataEntry(document.ContactUs.txtName,'Name')) return false;
//	if (!ForceDataEntry(document.ContactUs.txtgender,'Gender')) return false;
//	if (!ForceDataEntry(document.ContactUs.textAdd,'Address')) return false;
//	if (!ForceDataEntry(document.ContactUs.txtcity,'City')) return false;
//	if (!ForceNumericEntry(document.ContactUs.txtpin,'Pin')) return false;
//	if (!ForceDataEntry(document.ContactUs.txtEmail, 'Email')) return false;
//	//# Validating Email Address for Valid Format
//	if (!IsEmailValid(document.ContactUs.txtEmail,'Enter proper Email')) return false;
//	
//	
//	if (!ForceNumericEntry(document.ContactUs.txtPhone,'Phone No')) return false;
//	
//	
//}



function textCounter(field,cntfield,maxlimit) 
{
    if (field.value.length > maxlimit) // if too long...trim it!
        field.value = field.value.substring(0, maxlimit);
    // otherwise, update 'characters left' counter
    else
        cntfield.value = maxlimit - field.value.length;
}



function radio_button_checker(objField,strFieldName)
{
    // set var radio_choice to false
    var radio_choice = false;

    // Loop from zero to the one minus the number of radio button selections
     
    for (counter = 0; counter < objField.length; counter++)
    {
   
    // If a radio button has been selected it will return true
    // (If not it will return false)
    if (objField[counter].checked)
    radio_choice = true; 
    }

    if (!radio_choice)
    {
    // If there were no selections made display an alert box 
    alert("Please select "+strFieldName);
    return (false);
    }
    return (true);
}



function ValidateOrderSheet()
{
    if(!radio_button_checker(document.OrderSheet.radio_button,"PrintVersion/EBook"))return false;
	//# Validating Company Name for blank
	if (!ForceDataEntry(document.OrderSheet.txtName,'Name')) return false;
	if (!ForceDataEntry(document.OrderSheet.textAdd,'Address')) return false;
	if (!ForceDataEntry(document.OrderSheet.txtcity,'City')) return false;
	if (!ForceNumericEntry(document.OrderSheet.txtpin,'Pin')) return false;
	if (!ForceDataEntry(document.OrderSheet.txtEmail, 'Email')) return false;
	//# Validating Email Address for Valid Format
	if (!IsEmailValid(document.OrderSheet.txtEmail,'Enter proper Email')) return false;

	if (!ForceNumericEntry(document.OrderSheet.txtPhone,'Phone No')) return false;
	
	if(!radio_button_checker(document.OrderSheet.radio_Payment,"Payment Details"))return false;
	
	if(document.OrderSheet.radio_Payment[0].checked==true)
	{
	    if (!ForceDataEntry(document.OrderSheet.txtchequeno,'Cheque No')) return false;
	    if (!ForceDataEntry(document.OrderSheet.txtdate,'Date')) return false;
	    if (!ForceDecimalEntry(document.OrderSheet.txtamount,'Amount')) return false;
	    if (!ForceDataEntry(document.OrderSheet.txtdrawn,'Drawn on')) return false;
	}
	
	if(document.OrderSheet.radio_Payment[1].checked==true)
	{
	  if (!ForceDataEntry(document.OrderSheet.txtchequeno2,'Cheque No')) return false;
	    if (!ForceDataEntry(document.OrderSheet.txtdate2,'Date')) return false;
	    if (!ForceDecimalEntry(document.OrderSheet.txtamount2,'Amount')) return false;
	    if (!ForceDataEntry(document.OrderSheet.txtdrawn2,'Drawn on')) return false;
	}
	
	if(document.OrderSheet.radio_Payment[2].checked==true)
	{
	    if (!ForceDataEntry(document.OrderSheet.txtTransId,'Transacion Id')) return false;
	    if (!ForceDataEntry(document.OrderSheet.txtTransdate,'Transacion Date')) return false;
	    if (!ForceDecimalEntry(document.OrderSheet.txtTransAmount,'Transacion Amount')) return false;
	    if (!ForceDataEntry(document.OrderSheet.txtTransfer,'Transfer from')) return false;
	}
}
