// JavaScript Document

// THE SEARCH FORM VALIDATION ON THE HOME PAGE
function ValidateSearch(){
	var cat = document.getElementById('category').value;
	if(parseInt(cat) <= 0){
		alert('Please select atleast one category.');
		return false;
	}
	return true;
}

// THE NEW REGISTRATION FORM VALIDATION
function ValidateRegistration(){

	var email = trim(document.getElementById('email').value);
	if(!email.match(RegEx_Email)){
		alert('Please fill in a valid Email Address to register.');
		return false;
	}
	
	
	var pass = trim(document.getElementById('password').value);
	var len = pass.length;
	
	if(len <6){
		alert('Please fill in a valid password. Minimum 6 Characters.');
		return false;
	}
	
	var repass = trim(document.getElementById('repassword').value);
	if(pass != repass){
		alert('Your passwords do not match. Please retype password correctly.');
		return false;
	}	
	var fullname = trim(document.getElementById('fullname').value);
	if(fullname==''){
		alert('Please provide a valid Name for registration.');
		return false;
	}	
	//var cc = trim(document.getElementById('countrycode').value);
	var sc = trim(document.getElementById('stdcode').value);
	var phone = trim(document.getElementById('phone').value);
	phone = phone+sc;
	if(phone!='' && !phone.match(RegEx_Phone)){
		alert('Please provide a valid phone number.');
		return false;
	}	

	var mobile = trim(document.getElementById('mobile').value);
	if(mobile=='' || !mobile.match(RegEx_Mobile)){
		alert('Please provide a valid mobile number.');
		return false;
	}
	mobile = mobile.replace(/[()+\-]+/g,'');
	if(mobile.length<10){
		alert('Incorrect mobile number. Must contain atleast 10 digits.');
		return false;
	}
	
	var country = parseInt(document.getElementById('country').value);
	if(country < 1){
		alert('Please select a country.');
		return false;
	}
	
	var state = parseInt(document.getElementById('state').value);
	if(state < 1){
		alert('Please select a state.');
		return false;
	}
	
	var city = parseInt(document.getElementById('city').value);
	if(city < 1){
		alert('Please select a city.');
		return false;
	}
	

	var terms  = document.getElementById('terms');
	if(terms.checked==false){
		alert('You must agree to the terms and conditions to register with www.SecondHandMall.com');
		return false;
	}
	return true;
}

function ValidateLeftLogin(){
	var uid = trim(document.getElementById('username').value);
	if(uid == '' || uid.length < 6){
		alert('Please provide a valid Email to Sign In.');
		return false;
	}
	
	var pass = trim(document.getElementById('password').value);
	if(pass == '' || pass.length < 6){
		alert('Please provide a valid password to Sign In.');
		return false;
	}

	return true;	
}

function ValidateReferFriend(){
	
	var yourname = trim(document.getElementById("yourname").value);
	if(yourname=='Your Name'){
		alert('Please provide your name for your friend to know.');
		return false;
	}

	var youremail = trim(document.getElementById("youremail").value);
	if(!youremail.match(RegEx_Email)){
		alert('Please provide a valid email to your friend.');
		return false;
	}

	var friendsname = trim(document.getElementById("friendsname").value);
	
	if(friendsname=="Friend's Name"){
		alert('Please provide a friend\'s name.');
		return false;
	}

	var friendsemail = trim(document.getElementById("friendsemail").value);
	if(!friendsemail.match(RegEx_Email)){
		alert('Please provide a valid email to refer.');
		return false;
	}
	var message = escape(document.getElementById('refermessage').value);
	ReferSite_Ajax(yourname, youremail, friendsname, friendsemail, message,{'target':'REFER_SITE_ERROR','preloader':'PRELOADER_REFERSITE'});
	
	return true;
}

function ValidateProductRequest(){
	
	var title = trim(document.getElementById('title').value);
	if(title==''){
		alert('Please provide a valid Title for your product.');
		return false;
	}

	var category = document.getElementById('category').value;
	if(category < 1){
		alert('Please Select a category for your product request.');
		return false;
	}

	var description = (document.getElementById('description').value);
	if(description ==''){
		alert('Please fill in a valid description.');
		return false;
	}

	var fullname = trim(document.getElementById('fullname').value);
	if(fullname==''){
		alert('Please provide a valid name.');
		return false;
	}
	
	var email = trim(document.getElementById('email').value);
	if(!email.match(RegEx_Email)){
		alert('Please provide a valid email address for contact.');
		return false;				
	}

	var mobile = trim(document.getElementById('mobile').value);
	if(mobile=='' || !mobile.match(RegEx_Mobile)){
		alert('Please provide a valid mobile number.');
		return false;
	}
	mobile = mobile.replace(/[()+\-]+/g,'');
	if(mobile.length<10){
		alert('Incorrect mobile number. Must contain atleast 10 digits.');
		return false;
	}
	
	return true;
}


// THE CONTACT US FORM VALIDATION
function ValidateContactUs(){

	var name = trim(document.getElementById('name').value);
	if(name==''){
		alert('Please provide a valid name.');
		return false;
	}
	
	var email = trim(document.getElementById('email').value);
	if(!email.match(RegEx_Email)){
		alert('Please provide a valid email address.');
		return false;				
	}

	var mobile = trim(document.getElementById('mobile').value);
	if(mobile=='' || !mobile.match(RegEx_Mobile)){
		alert('Please provide a valid mobile number.');
		return false;
	}
	mobile = mobile.replace(/[()+\-]+/g,'');
	if(mobile.length<10){
		alert('Incorrect mobile number. Must contain atleast 10 digits.');
		return false;
	}
	
	var comment = (document.getElementById('comment').value);
	if(comment==''){
		alert('Please fill in a valid comment.');
		return false;
	}
	return true;
}

// ====================================================================================================================================================//
//	THIS IS WHERE ALL THE UPLOAD FORMS VALIDATIONS BEGIN
// ====================================================================================================================================================//
function ValidateSellProduct(){

	var title = trim(document.getElementById('title').value);
	if(title=='' || title.length<3){
		alert('Please input a valid title for your product. Min 3 Chars.');
		return false;
	}
	
	
	//var desc = trim(document.getElementById('desc').value).length;
	var desc = tinyMCE.get('desc').getContent()
	if(desc.length<1){
		alert('Please input a valid description for your product.');
		return false;
	}
	var condition = document.getElementById('condition').value;
	if(parseInt(condition)==2 || parseInt(condition)==3){
		var years = parseInt(document.getElementById('years').value);
		var months = parseInt(document.getElementById('months').value);
		if(years==0 && months==0){
			alert('Please specify how old the product is.');
			return false;
		}
	}

	var currency = parseInt(document.getElementById('currency').value);
	if(currency < 1){
		alert('Please select a currency to sell the product.');
		return false;
	}

	/*	REMOVE THIS COMMENT IN CASE MARKET PRICE FOR ANY PRODUCT EVER IS MANDATORY
	var marketprice = parseInt(document.getElementById('marketprice').value);
	if(marketprice!='' && isNaN(marketprice)){
		alert('Please provide a valid market price or keep it blank.');
		return false;
	} 
	*/
	var marketprice = document.getElementById('marketprice').value;
	if(marketprice!=''){
		if(IsNumeric(marketprice) == false) 
		{
			alert('Please provide a valid market price or keep it blank.');
			return false;
		}
	} 
	
	var offerprice = document.getElementById('offerprice').value;
	if(offerprice==''){
		alert('Please enter offer price.');
		return false;
	}else
	if(offerprice!=''){
		//if(isNaN(offerprice) || offerprice<0){
		if(IsNumeric(offerprice) == false){
			//alert('Please provide an offer price at which you wish to sell the product.');
			alert('Offer price field allows only whole numbers');
			return false;
		}
	}
	var category = parseInt(document.getElementById('category').value);
	if(category < 1){
		alert('Please select a category for the product.');
		return false;
	}

	var country = parseInt(document.getElementById('country').value);
	
	if(country < 1){
		alert('Please select a country.');
		return false;
	}
	if(document.getElementById('shmagent').value==0){
	
		var state = parseInt(document.getElementById('state').value);
		if(state < 1){
			alert('Please select a state.');
			return false;
		}
		
		var city = parseInt(document.getElementById('city').value);
		if(city < 1){
			alert('Please select a city.');
			return false;
		}
	}else if(document.getElementById('shmagent').value==1){
		var state = (document.getElementById('ids_states').value);
		
		if(state == '' || state == 'null'){
			alert('Please select a state.');
			return false;
		}
		
		var city = (document.getElementById('ids_cities').value);
		if(city == '' || city == 'null'){
			alert('Please select a city.');
			return false;
	}
	}
	var shipping = parseInt(document.getElementById('shipping').value);
	if(shipping==0){
		alert('Please select shipping as applicable');
		return false;
	}
	
	var shipping = parseInt(document.getElementById('shipping').value);
	if(shipping==0){
		alert('Please select shipping as applicable');
		return false;
	}

	if(document.getElementById('verified')){
		checkval = document.getElementById('verified').checked;
		verificationcomments=trim(document.getElementById('verificationcomments').value);
		if(checkval==true && verificationcomments==''){
			alert('Please provide verification Comments');
			return false;
		}
		
		
	}
	return true;
}



function ValidateRentProduct(){
	var title = trim(document.getElementById('title').value);
	if(title=='' || title.length<3){
		alert('Please input a valid title for your product. Min 3 Chars.');
		return false;
	}
	
	//var desc = trim(document.getElementById('desc').value).length;
	var desc = tinyMCE.get('desc').getContent()
	if(desc.length<1){
		alert('Please input a valid description for your product.');
		return false;
	}

	var currency = parseInt(document.getElementById('currency').value);
	if(currency < 1){
		alert('Please select a currency to rent the product.');
		return false;
	}	
	
	var rent = trim(document.getElementById('rent').value);
	if(rent==''){
		alert('Please specify the rental basis for your product.');
		return false;
	}
	
	if(IsNumeric(rent) == false){
		alert('Only numbers are allowed under Rental Basis.');
		return false;
	}
	
	var category = parseInt(document.getElementById('category').value);
	if(category < 1){
		alert('Please select a category for the product.');
		return false;
	}

	var country = parseInt(document.getElementById('country').value);
	if(country < 1){
		alert('Please select a country.');
		return false;
	}
	
	var state = parseInt(document.getElementById('state').value);
	if(state < 1){
		alert('Please select a state.');
		return false;
	}
	
	var city = parseInt(document.getElementById('city').value);
	if(city < 1){
		alert('Please select a city.');
		return false;
	}
	
	if(document.getElementById('verified')){
		checkval = document.getElementById('verified').checked;
		verificationcomments=trim(document.getElementById('verificationcomments').value);
		if(checkval==true && verificationcomments==''){
			alert('Please provide verification Comments');
			return false;
		}
	}
	return true;
}

function ValidateDonateProduct(){
	var title = trim(document.getElementById('title').value);
	if(title=='' || title.length<3){
		alert('Please input a valid title for your product. Min 3 Chars.');
		return false;
	}
	
	//var desc = trim(document.getElementById('desc').value).length;
	var desc = tinyMCE.get('desc').getContent()
	if(desc.length<1){
		alert('Please input a valid description for your product.');
		return false;
	}

	var category = parseInt(document.getElementById('category').value);
	if(category < 1){
		alert('Please select a category for the product.');
		return false;
	}

	var country = parseInt(document.getElementById('country').value);
	if(country < 1){
		alert('Please select a country.');
		return false;
	}
	
	var state = parseInt(document.getElementById('state').value);
	if(state < 1){
		alert('Please select a state.');
		return false;
	}
	
	var city = parseInt(document.getElementById('city').value);
	if(city < 1){
		alert('Please select a city.');
		return false;
	}
	
	var donateto = parseInt(document.getElementById('donateto').value);
	if(donateto==0){
		alert('Please select an option as to whoom to donate to.');
		return false;
	}
	
	if(document.getElementById('verified')){
		checkval = document.getElementById('verified').checked;
		verificationcomments=trim(document.getElementById('verificationcomments').value);
		if(checkval==true && verificationcomments==''){
			alert('Please provide verification Comments');
			return false;
		}
	}	
	return true;
}

function ValidateDisposeProduct(){
	
	var title = trim(document.getElementById('title').value);
	if(title=='' || title.length<3){
		alert('Please input a valid title for your product. Min 3 Chars.');
		return false;
	}
	
	//var desc = trim(document.getElementById('desc').value).length;
	var desc = tinyMCE.get('desc').getContent()
	if(desc.length<1){
		alert('Please input a valid description for your product.');
		return false;
	}

	var category = parseInt(document.getElementById('category').value);
	if(category < 1){
		alert('Please select a category for the product.');
		return false;
	}

	var country = parseInt(document.getElementById('country').value);
	if(country < 1){
		alert('Please select a country.');
		return false;
	}
	
	var state = parseInt(document.getElementById('state').value);
	if(state < 1){
		alert('Please select a state.');
		return false;
	}
	
	var city = parseInt(document.getElementById('city').value);
	if(city < 1){
		alert('Please select a city.');
		return false;
	}
	
	var payfees = document.getElementById('payfees').checked;
	if(payfees){
		var currency = parseInt(document.getElementById('currency').value);
		if(currency < 1){
			alert('Please select a currency for the product.');
			return false;
		}
		var field = document.getElementById('disposalfees');
		if(field.value=='' || field.value=="0"){
			alert('Please fill in some recycle fees that you are ready to pay.');
			return false;
		}
		  var check = true;
          var value = field.value; //get characters
          //check that all characters are digits, ., -, or ""
          for(var i=0;i < field.value.length; ++i)
          {
               var new_key = value.charAt(i); //cycle through characters
               if(((new_key < "0") || (new_key > "9")) && !(new_key == ""))
               {
                    check = false;
                    break;
               }
          }
		  
		 if(!check)
         {
			alert('Please fill only numbers as recycle fees that you are ready to pay.');
			return false; 	
		 }
	}
	
	if(document.getElementById('verified')){
		checkval = document.getElementById('verified').checked;
		verificationcomments=trim(document.getElementById('verificationcomments').value);
		if(checkval==true && verificationcomments==''){
			alert('Please provide verification Comments');
			return false;
		}
	}
	
	return true;
}

function ValidateExchangeProduct(){

	var title = trim(document.getElementById('title').value);
	if(title=='' || title.length<3){
		alert('Please input a valid title for your product. Min 3 Chars.');
		return false;
	}
	
	//var desc = trim(document.getElementById('desc').value).length;
	var desc = tinyMCE.get('desc').getContent()
	//alert(desc);
	if(desc.length<1){
		alert('Please input a valid description for your product.');
		return false;
	}

	var category = parseInt(document.getElementById('category').value);
	if(category < 1){
		alert('Please select a category for the product.');
		return false;
	}
	
	var extitle = trim(document.getElementById('extitle').value).length;
	if(extitle==0 || extitle<3){
		alert('Please input a valid title for product you want in exchange. Min 3 Chars.');
		return false;
	}
	
	var exdesc = trim(document.getElementById('exdesc').value);
	
	if(exdesc==''){
		alert('Please input a valid description for product you want in exchange.');
		return false;
	}

	var excategory = parseInt(document.getElementById('excategory').value);

	if(excategory < 1){
		alert('Please select a exchange category for the product you want in exchange.');
		return false;
	}

	var country = parseInt(document.getElementById('country').value);
	if(country < 1){
		alert('Please select a country.');
		return false;
	}
	
	var state = parseInt(document.getElementById('state').value);
	if(state < 1){
		alert('Please select a state.');
		return false;
	}
	
	var city = parseInt(document.getElementById('city').value);
	if(city < 1){
		alert('Please select a city.');
		return false;
	}
	
	if(document.getElementById('verified')){
		checkval = document.getElementById('verified').checked;
		verificationcomments=trim(document.getElementById('verificationcomments').value);
		if(checkval==true && verificationcomments==''){
			alert('Please provide verification Comments');
			return false;
		}
	}
	return true;
}


// VALIDATION FOR THE LOGIN FORM
function ValidateLogin(){
	
	var uid = trim(document.getElementById('username').value);
	if(uid == '' || uid.length < 6){
		alert('Please provide a valid Email to Sign In.');
		return false;
	}
	
	var pass = trim(document.getElementById('password').value);
	if(pass == '' || pass.length < 6){
		alert('Please provide a valid password to Sign In.');
		return false;
	}
	return true;
}

function ValidateAssociateRequest(){

	var fname = trim(document.getElementById('fname').value);
	if(fname==''){
		alert('Please provide a valid First Name for registration.');
		return false;
	}	

	var lname = trim(document.getElementById('lname').value);
	if(lname==''){
		alert('Please provide a valid Last Name for registration.');
		return false;
	}	
	
	var email = trim(document.getElementById('email').value);
	if(!email.match(RegEx_Email)){
		alert('Please fill in a valid Email Address.');
		return false;
	}

	var mobile = trim(document.getElementById('mobile').value);
	if(mobile=='' || !mobile.match(RegEx_Mobile)){
		alert('Please provide a valid mobile number.');
		return false;
	}
	
	/*
	mobile = mobile.replace(/[()+\-]+/g,'');
	if(mobile.length!=10){
		alert('Wrong mobile number. Must contain only 10 digits.');
		return false;
	}
	*/
	var phone = trim(document.getElementById('phone').value);
	phone = phone;
	if(phone!='' && !phone.match(RegEx_Mobile)){
		alert('Please provide a valid phone number.');
		return false;
	}	
	
	var country = trim(document.getElementById('country').value);
	if(country.length < 3){
		alert('Please provide your country of residence.');
		return false;
	}else	
	if(IsStr(country) == false) 
	{
		alert('Please provide correct country of residence.');
		return false;
	}
	
	var state = trim(document.getElementById('state').value);
	if(state.length < 3){
		alert('Please provide a valid state name.');
		return false;
	}else	
	if(IsStr(state) == false) 
	{
		alert('Please provide correct state of residence.');
		return false;
	}
	
	var city = trim(document.getElementById('city').value);
	if(city.length < 3){
		alert('Please provide a valid city name.');
		return false;
	}else	
	if(IsStr(city) == false) 
	{
		alert('Please provide correct city of residence.');
		return false;
	}
	
	return true;
}

function IsNumeric(strString)
{ 
var strValidChars = "0123456789";
var strChar;
var blnResult = true;

if (strString.length == 0) return false;

//  test strString consists of valid characters listed above
for (i = 0; i < strString.length && blnResult == true; i++)
  {
  strChar = strString.charAt(i);
  if (strValidChars.indexOf(strChar) == -1)
	 {
	 	blnResult = false;
	 }
  }
return blnResult;
}

function IsStr(strString)
{ 
var strValidChars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
var strChar;
var blnResult = true;

if (strString.length == 0) return false;

//  test strString consists of valid characters listed above
for (i = 0; i < strString.length && blnResult == true; i++)
  {
  strChar = strString.charAt(i);
  if (strValidChars.indexOf(strChar) == -1)
	 {
	 	blnResult = false;
	 }
  }
return blnResult;
}



function validateSearchbyPID(){
	var pid=document.getElementById('pid').value;
	var checkSpace =/^\s+$/
	var error = 0;
	var msg ='';
	
	if(pid=='' || checkSpace.test(pid)){
		error=1;
		msg += "Please Enter the Product Number \n";
	}
	
	if(pid!=''){ 
		if(IsNumeric(pid) == false) 
		{
			error=1;	
			msg += "Please Enter Valid Product Number!";
			document.getElementById('pid').value='';
		}
	}
	
	if(error==1){
		alert(msg);
		return false;
	}else{
		return true;
	}
}
