function copyToClipBoard(sContents)
{
	window.clipboardData.setData("Text", sContents);
	alert("The code has been copied to your clipboard");
}

function RTrim(VALUE) {
	var w_space = String.fromCharCode(32);
	var v_length = VALUE.length;
	var strTemp = "";
	if(v_length < 0){
		return"";
	}
	var iTemp = v_length -1;

	while(iTemp > -1){
		if(VALUE.charAt(iTemp) == w_space){
			;
		}
		else{
			strTemp = VALUE.substring(0,iTemp +1);
			break;
		}
		iTemp = iTemp-1;

	} //End While

	return strTemp;
}

function LTrim(VALUE){
	var w_space = String.fromCharCode(32);
	if(v_length < 1){
		return"";
	}

	var v_length = VALUE.length;
	var strTemp = "";

	var iTemp = 0;

	while(iTemp < v_length){
		if(VALUE.charAt(iTemp) == w_space){
			;
		}
		else{
			strTemp = VALUE.substring(iTemp,v_length);
			break;
		}
		iTemp = iTemp + 1;
	} //End While
	return strTemp;
}


function Trim(TRIM_STR) {
	if(TRIM_STR.length < 1) {
		return"";
	}
	TRIM_STR = RTrim(TRIM_STR);
	TRIM_STR = LTrim(TRIM_STR);

	if(TRIM_STR=="") {
		return "";
	}
	else {
		return TRIM_STR;
	}
}

function replaceSubstring(inputString, fromString, toString) {
   // Goes through the inputString and replaces every occurrence of fromString with toString
   var temp = inputString;
   var toTheLeft;
   var toTheRight;
   if (fromString == "") {
      return inputString;
   }
   if (toString.indexOf(fromString) == -1) { // If the string being replaced is not a part of the replacement string (normal situation)
      while (temp.indexOf(fromString) != -1) {
         toTheLeft = temp.substring(0, temp.indexOf(fromString));
         toTheRight = temp.substring(temp.indexOf(fromString)+fromString.length, temp.length);
         temp = toTheLeft + toString + toTheRight;
      }
   } else { // String being replaced is part of replacement string (like "+" being replaced with "++") - prevent an infinite loop
      var midStrings = new Array("~", "`", "_", "^", "#");
      var midStringLen = 1;
      var midString = "";
      // Find a string that doesn't exist in the inputString to be used
      // as an "inbetween" string
      while (midString == "") {
         for (var i=0; i < midStrings.length; i++) {
            var tempMidString = "";
            for (var j=0; j < midStringLen; j++) { tempMidString += midStrings[i]; }
            if (fromString.indexOf(tempMidString) == -1) {
               midString = tempMidString;
               i = midStrings.length + 1;
            }
         }
      } // Keep on going until we build an "inbetween" string that doesn't exist
      // Now go through and do two replaces - first, replace the "fromString" with the "inbetween" string
      while (temp.indexOf(fromString) != -1) {
         toTheLeft = temp.substring(0, temp.indexOf(fromString));
         toTheRight = temp.substring(temp.indexOf(fromString)+fromString.length, temp.length);
         temp = toTheLeft + midString + toTheRight;
      }
      // Next, replace the "inbetween" string with the "toString"
      while (temp.indexOf(midString) != -1) {
         toTheLeft = temp.substring(0, temp.indexOf(midString));
         toTheRight = temp.substring(temp.indexOf(midString)+midString.length, temp.length);
         temp = toTheLeft + toString + toTheRight;
      }
   } // Ends the check to see if the string being replaced is part of the replacement string or not
   return temp; // Send the updated string back to the user
} // Ends the "replaceSubstring" function

function echeck(str) {

		var at="@"
		var dot="."
		var lat=str.indexOf(at)
		var lstr=str.length
		var ldot=str.indexOf(dot)
		if (str.indexOf(at)==-1){
		   alert("Invalid E-mail address")
		   return false
		}

		if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
		   alert("Invalid E-mail address")
		   return false
		}

		if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
		    alert("Invalid E-mail address")
		    return false
		}

		 if (str.indexOf(at,(lat+1))!=-1){
		    alert("Invalid E-mail address")
		    return false
		 }

		 if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
		    alert("Invalid E-mail address")
		    return false
		 }

		 if (str.indexOf(dot,(lat+2))==-1){
		    alert("Invalid E-mail address")
		    return false
		 }
		
		 if (str.indexOf(" ")!=-1){
		    alert("Invalid E-mail address")
		    return false
		 }

 		 return true					
}

function validateSendToFriendFrm(frm) {
	if (echeck(frm.email.value)) {
		frm.butSubmit.value='Please wait...';
		frm.butSubmit.disabled=true;
		return true;
	}
	else {
		frm.email.focus();
		return false;
	}
}

function validateLoginFrm(frm) {
	if (frm.username.value.length<3) {
		alert("Please ensure that your username is at least 3 characters long!");
		frm.username.focus();
		return false;
	}
	else if (frm.password.value.length<3) {
		alert("Please ensure that your password is at least 3 characters long!");
		frm.password.focus();
		return false;
	}
	else {
		frm.butSubmit.value='Please wait...';
		frm.butSubmit.disabled=true;
		return true;
	}
}

function validateUpdateUserFrm(frm) {
	if (frm.password.value.length > 0 && (frm.password.value.length<3 || (frm.password.value!=frm.passwordverify.value))) {
		alert("Please ensure that your password is at least 3 characters long\nand make sure that both password fields are filled with the SAME EXACT password!\nIf you do not want to update your password, please clear both password fields.");
		frm.password.focus();
		return false;
	}
	else if (frm.firstname.value.length<2) {
		alert("Please ensure that your first name is at least 2 characters long!");
		frm.firstname.focus();
		return false;
	}
	else if (frm.surname.value.length<2) {
		alert("Please ensure that your surname is at least 2 characters long!");
		frm.surname.focus();
		return false;
	}
	else if (frm.email.value.length<6) {
		alert("Please ensure that your e-mail address is filled in using the correct format!");
		frm.email.focus();
		return false;
	}
	else if (!echeck(frm.email.value)) {
		frm.email.focus();
		return false;
	}
	else if (!frm.butTermsAgree.checked) {
		alert("You must accept the Terms and Conditions of this service by ticking the checkbox in order to proceed further.");
		frm.butTermsAgree.focus();
		return false;
	}
	else if (!check_date(frm.dobDay.value + "." + frm.dobMonth.value + "." + frm.dobYear.value)) {
		alert("Please select a valid Date of Birth!");
		frm.dobDay.focus();
		return false;
	}
	else if (frm.town.value.length<2) {
		alert("Please ensure that you have specified a city/town of origin!");
		frm.town.focus();
		return false;
	}
	else if (frm.countryID.selectedIndex ==0 || frm.countryID.selectedIndex== 1 || frm.countryID.selectedIndex== 12) {
		alert("Please ensure that you have chosen a country of origin!");
		frm.countryID.focus();
		return false;
	}
	else {
		frm.butSubmit.value='Please wait...';
		frm.butSubmit.disabled=true;
		return true;
	}
}

function validatePost(frm) {
	if (frm.postContent.value.length==0) {
		alert("Oh, come on! Type some stuff in there before you submit the post!");
		frm.postContent.focus();
		return false;
	}
	else {
		if (frm.postSubject.value.length==0) {
			frm.postSubject.value = "(no subject)";
		}
		frm.butSubmit.value='Please wait...';
		frm.butSubmit.disabled=true;
		return true;
	}
}

function validateSearchFrm(frm) {
	frm.keyword.value = replaceSubstring(frm.keyword.value, '<', '');
	frm.keyword.value = replaceSubstring(frm.keyword.value, '>', '');
	frm.keyword.value = replaceSubstring(frm.keyword.value, '[', '');
	frm.keyword.value = replaceSubstring(frm.keyword.value, ']', '');
	frm.keyword.value = replaceSubstring(frm.keyword.value, '..', '');
	frm.keyword.value = replaceSubstring(frm.keyword.value, '...', '');
	frm.keyword.value = replaceSubstring(frm.keyword.value, ';', '');
	frm.keyword.value = replaceSubstring(frm.keyword.value, '&', '');
	frm.keyword.value = replaceSubstring(frm.keyword.value, '+', '');
	frm.keyword.value = replaceSubstring(frm.keyword.value.toLowerCase(), ' and ', '');
	frm.keyword.value = replaceSubstring(frm.keyword.value.toLowerCase(), ' or ', '');
	
	if (frm.keyword.value.length<3 || frm.keyword.value == 'Give me a search keyword') {
		alert("Please ensure that the keyword is at least 3 characters long!");
		frm.keyword.focus();
		return false;
	}
	else {
		frm.butSubmit.value='Please wait...';
		frm.butSubmit.disabled=true;
		return true;
	}
}

function validateSignupFrm(frm) {
	if (frm.username.value.length<3) {
		alert("Please ensure that your username is at least 3 characters long!");
		frm.username.focus();
		return false;
	}
	else if (frm.password.value.length<3 || (frm.password.value!=frm.passwordverify.value)) {
		alert("Please ensure that your password is at least 3 characters long\nand make sure that both password fields are filled with the SAME EXACT password!");
		frm.password.focus();
		return false;
	}
	else if (frm.firstname.value.length<2) {
		alert("Please ensure that your first name is at least 2 characters long!");
		frm.firstname.focus();
		return false;
	}
	else if (frm.surname.value.length<2) {
		alert("Please ensure that your surname is at least 2 characters long!");
		frm.surname.focus();
		return false;
	}
	else if (frm.email.value.length<6) {
		alert("Please ensure that your e-mail address is filled in using the correct format!");
		frm.email.focus();
		return false;
	}
	else if (!echeck(frm.email.value)) {
		frm.email.focus();
		return false;
	}
	else if (!frm.butTermsAgree.checked) {
		alert("You must accept the Terms and Conditions of this service by ticking the checkbox in order to proceed further.");
		frm.butTermsAgree.focus();
		return false;
	}
	else if (!check_date(frm.dobDay.value + "." + frm.dobMonth.value + "." + frm.dobYear.value)) {
		alert("Please select a valid Date of Birth!");
		frm.dobDay.focus();
		return false;
	}
	else if (frm.town.value.length<2) {
		alert("Please ensure that you have specified a city/town of origin!");
		frm.town.focus();
		return false;
	}
	else if (frm.countryID.selectedIndex ==0 || frm.countryID.selectedIndex== 1 || frm.countryID.selectedIndex== 12) {
		alert("Please ensure that you have chosen a country of origin!");
		frm.countryID.focus();
		return false;
	}
	else {
		frm.butSubmit.value='Please wait...';
		frm.butSubmit.disabled=true;
		return true;
	}
}

function updateILinkCode(frm) {
	var iLinkCode;
	iLinkCode = frm.origIFrameCode.value;
	iLinkCode = replaceSubstring(iLinkCode, "$w", replaceSubstring(Trim(frm.ifWidth.value), " ", ""));
	iLinkCode = replaceSubstring(iLinkCode, "$h", replaceSubstring(Trim(frm.ifHeight.value), " ", ""));
	iLinkCode = replaceSubstring(iLinkCode, "$b", replaceSubstring(Trim(frm.ifBorder.value), " ", ""));
	iLinkCode = replaceSubstring(iLinkCode, "$im", replaceSubstring(Trim(frm.ifBGImage.value), " ", ""));
	iLinkCode = replaceSubstring(iLinkCode, "$col", replaceSubstring(replaceSubstring(Trim(frm.ifBGColour.value), " ", ""), "#", ""));
	frm.iframelink.value = iLinkCode;
}

function populatePhoneModels(brandList) {
	var i=0;
	modelList = brandList.form.phoneModel;
	
	for (i=modelList.options.length-1; i>0; i--) {
		modelList.options[i]=null;
	}

	if (brandList.selectedIndex>0) {
		var brandArray=brandList.options[brandList.selectedIndex].value.split("@@@");
		var modelArray=brandArray[1].split("$$$");
		
		for (i=0; i<modelArray.length-1; i++) {
			newOption=new Option();
   			newOption.text=modelArray[i];
   			newOption.value=brandArray[0] + "  " + modelArray[i];
			modelList.options[modelList.options.length]=newOption;
		}
	}
}

function populateCountryProviders(countryList) {
	var i=0;
	networkList = countryList.form.network;
	
	for (i=networkList.options.length-1; i>0; i--) {
		networkList.options[i]=null;
	}

	if (countryList.selectedIndex>0) {
		var countryArray=countryList.options[countryList.selectedIndex].value.split("@@@");
		var networkArray=countryArray[1].split("$$$");
		
		newOption=new Option();
		newOption.text="Other network - Not listed";
		newOption.value=countryArray[0] + "  Other network - Not listed"; 
		networkList.options[networkList.options.length]=newOption;
		
		for (i=0; i<networkArray.length-1; i++) {
			newOption=new Option();
   			newOption.text=networkArray[i];
   			newOption.value=countryArray[0] + "  " + networkArray[i];
			networkList.options[networkList.options.length]=newOption;
		}
		
		if (networkList.options.length>10) {
			newOption=new Option();
			newOption.text="Other network - Not listed";
			newOption.value=countryArray[0] + "  Other network - Not listed"; 
			networkList.options[networkList.options.length]=newOption;
		}
	}
}

function selectPayMethod(payMethod, frm) {
	frm.form.paymentMethod.value=payMethod;
	if (frm.form.phoneBrand.selectedIndex>0 && frm.form.phoneModel.selectedIndex>0 && frm.form.country.selectedIndex>0 && frm.form.network.selectedIndex>0 && frm.form.IMEI.value.length>13 && echeck(frm.form.email.value)) {
		alert("ALL YOUR DETAILS ARE NOW READY TO BE SUBMITTED SO THAT WE CAN BEGIN THE UNLOCK PROCESS OF YOUR PHONE. WHEN YOU CLICK 'OK' YOU WILL BE TRANSFERRED TO A SECURE SERVER OPERATED BY '" + payMethod + "' SO THAT YOU CAN PAY SECURELY BY CREDIT CARD. WE WILL NEVER BE ABLE TO SEE OR STORE YOUR CREDIT CARD DETAILS ANYWHERE! ALL BILLING OPERATIONS ARE HANDLED DIRECTLY BY '" + payMethod + "' AND WE SIMPLY GET A 'YES' OR A 'NO' AS A RESPONSE FROM THEM IN ORDER TO PROCEED WITH YOUR REQUEST. WHEN PAYMENT IS COMPLETE, YOU WILL BE RETURNED TO THIS SITE AND YOU WILL RECEIVE AN E-MAIL WITH A CONFIRMATION OF YOUR ORDER, AND A TRACKING NUMBER. IN A FEW MOMENTS YOU WILL ALSO RECEIVE A SECOND E-MAIL WITH YOUR UNLOCK CODE AND INSTRUCTIONS.");
		frm.form.submit();
	}
	else {
		alert("Please make sure that you have filled in all parts of this page and have provided us with your FULL IMEI number and a valid e-mail address");
		return false;
	}
}

function validateUpload(frm) {
	if (frm.imgfile.value.length>0) {
		frm.uplImgBut.value='Please wait';
		frm.uplImgBut.disabled=true;
		return true;
	}
	else {
		alert("You have not chosen a file to upload!");
		return false;
	}
}

function check_date(DateValue){
	var checkstr = "0123456789";
	var DateTemp = "";
	var seperator = ".";
	var day;
	var month;
	var year;
	var leap = 0;
	var err = 0;
	var i;
   err = 0;

   /* Delete all chars except 0..9 */
   for (i = 0; i < DateValue.length; i++) {
	  if (checkstr.indexOf(DateValue.substr(i,1)) >= 0) {
	     DateTemp = DateTemp + DateValue.substr(i,1);
	  }
   }
   DateValue = DateTemp;
   /* Always change date to 8 digits - string*/
   /* if year is entered as 2-digit / always assume 20xx */
   if (DateValue.length == 6) {
      DateValue = DateValue.substr(0,4) + '20' + DateValue.substr(4,2); }
   if (DateValue.length != 8) {
      err = 19;}
   /* year is wrong if year = 0000 */
   year = DateValue.substr(4,4);
   if (year == 0) {
      err = 20;
   }
   /* Validation of month*/
   month = DateValue.substr(2,2);
   if ((month < 1) || (month > 12)) {
      err = 21;
   }
   /* Validation of day*/
   day = DateValue.substr(0,2);
   if (day < 1) {
     err = 22;
   }
   /* Validation leap-year / february / day */
   if ((year % 4 == 0) || (year % 100 == 0) || (year % 400 == 0)) {
      leap = 1;
   }
   if ((month == 2) && (leap == 1) && (day > 29)) {
      err = 23;
   }
   if ((month == 2) && (leap != 1) && (day > 28)) {
      err = 24;
   }
   /* Validation of other months */
   if ((day > 31) && ((month == "01") || (month == "03") || (month == "05") || (month == "07") || (month == "08") || (month == "10") || (month == "12"))) {
      err = 25;
   }
   if ((day > 30) && ((month == "04") || (month == "06") || (month == "09") || (month == "11"))) {
      err = 26;
   }
   /* if 00 ist entered, no error, deleting the entry */
   if ((day == 0) && (month == 0) && (year == 00)) {
      err = 0; day = ""; month = ""; year = ""; seperator = "";
   }
   /* if no error, write the completed date to Input-Field (e.g. 13.12.2001) */
   if (err == 0) {
      return true;
   }
   /* Error-message if err != 0 */
   else {
      return false;
   }
}

function isAlphanumeric(e)
{
	var key;
	var keychar;
	
	if (window.event)
	   key = window.event.keyCode;
	else if (e)
	   key = e.which;
	else
	   return true;
	keychar = String.fromCharCode(key);
	keychar = keychar.toLowerCase();
	
	// control keys
	if ((key==null) || (key==0) || (key==8) || 
	    (key==9) || (key==13) || (key==27) )
	   return true;
	
	// alphas and numbers
	else if ((("abcdefghijklmnopqrstuvwxyz0123456789_.-").indexOf(keychar) > -1))
	   return true;
	else
	   return false;
}