// reg/rjs/validat_step1.js

function findObj(n, d)
{
  var p, i, x;
  if(!d) d=document;
  if((p=n.indexOf("?"))>0&&parent.frames.length)
  {
     d=parent.frames[n.substring(p+1)].document;
     n=n.substring(0,p);
  }
  if(!(x=d[n])&&d.all)        x=d.all[n];
  for (i=0;!x&&i<d.forms.length;i++)        x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++)        x=findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n);
  return x;
}

function formValidate(forma,log_min,pas_min,pas_max) {
	var errors='';
	if(forma.elements['first_name'].value == "") {
		errors+='- First Name is required.\n';
	}
	if(forma.elements['last_name'].value == "") {
		errors+='- Last Name is required.\n';
	}
	if(forma.elements['email'].value == "") {
		errors+='- E-mail is required.\n';
	}else {
		val=forma.elements['email'].value;
		p=val.indexOf('@');
		q=val.indexOf('.');
		if (p<1 || p==(val.length-1) || q<1|| q==(val.length-1)) {
			errors+='- E-mail must contain a valid email address.\n';
		}
	} 
	
	/// ValidateLogin
	if(forma.elements['user_name'].value == "" || forma.elements['user_name'].value.length < log_min) {
		errors+='- Username is required and length should be not less than '+log_min+' characters.\n';
	}
	if(forma.elements['password'].value == "" || forma.elements['password'].value.length < pas_min || forma.elements['password'].value.length > pas_max)
	{ // Check Password
		errors+='- Password is required and length should be between '+pas_min+' and '+pas_max+' characters.\n';
	}else if(forma.elements['cpassword'].value == "" || forma.elements['cpassword'].value.length < pas_min || forma.elements['cpassword'].value.length > pas_max)
	{
		errors+='- Confirm Password is required and length should be between '+pas_min+' and '+pas_max+' characters.\n';
	} else if( forma.elements['password'].value != forma.elements['cpassword'].value ) {
		errors+='- Password entered does not match, please reenter your Password.\n';
	} else if( forma.elements['user_name'].value == forma.elements['password'].value ) {
		errors+='- Password must be different from your `Username`.\n';
	}
	if(forma.elements['birth_month'].value == "" || forma.elements['birth_day'].value == "" || forma.elements['birth_year'].value == "") {
		errors+='- Birthday is required.\n';
	}
	val=findObj('ss_captcha');
	if ( val ){
       	captcha = val.value;
       	if(captcha == "") {
			errors+='- Please enter the capture to verify your information.\n';
		}
	}
	
	// Check location
	var city = "",state = "",zip_code = "";
	val=findObj('city');
	if (val && val.value!=""){
       	city = val.value;
	}
	val=findObj('state');
	if (val && val.value!=""){
       	state = val.value;
	}
	val=findObj('zip_code');
	if (val && val.value!=""){
       	zip_code = val.value;
	}

	if(forma.elements['country_code'].value == "US") {
		if(zip_code=="" && city=="" && state=="" ){
			errors+='- Please Enter Zip Code or Choose the City and State.\n';
		} else if(zip_code !="") {
			if( city=="" && state=="" ){
				if(isNumber(zip_code)==0 || zip_code.length!=5) {
					errors+='- Please Enter valid Zip Code.\n';
				}
			} else if( (city!="" && state=="") || (city=="" && state!="") ){
				errors+='- Please Enter valid Zip Code or select State and City.\n';
			}
		} else if( (city!="" && state=="") || (city=="" && state!="") ){
			errors+='- Please Enter valid Zip Code or select State and City.\n';
		}
	} else if(city == "") {
		errors+='- City is required';
	}

	if( !forma.elements['confirm_flag'].checked ) {
		errors+='\nBefore registering you must read and agree to the Terms and Conditions and Privacy Policy.\n';
	}

	if (errors) alert('Please fill-in the following field(s) to register with Supersocial:\n'+errors+'\n');
	
	document.returnValue = (errors == '');
}

// Check, data is numeric
function isNumber(data) {
	var numStr="0123456789";
	var thisChar;
	var counter=0;
	for (var i=0; i<data.length; i++) {
		thisChar = data.substring(i, i+1);
		if (numStr.indexOf(thisChar) != -1)
			counter++;
	}
	if (counter == data.length) { //	alert("OK! It's number.");
		return 1;
	} else { //	alert("Data is not number. Ckeck!");
		return 0;
	}
}

