// $Id: profiles_scripts.js 968 2005-11-28 14:23:16Z zeke $ 

function switch_profile(profile_id)
{
    window.location=index_script+'?'+target_name+'=profiles&'+mode_name+'=update&profile_id='+profile_id;
}

function create_profile()
{
    window.location=index_script+'?'+target_name+'=profiles&'+mode_name+'=update&'+action_name+'=add_profile';
}

function delete_profile(profile_id)
{
    window.location=index_script+'?'+target_name+'=profiles&'+mode_name+'=update&'+action_name+'=delete_profile&profile_id='+profile_id;
}

//
// This function set required field mark
//
function fn_set_required_marks()
{
	for (field in required_fields) {
		if (document.getElementById(field) && document.getElementById('mark_'+field)) {
			document.getElementById('mark_'+field).innerHTML = "*";
		}
	}
}

//
// This function checks required fields and set a mark if something wrong
//
function fn_check_fields()
{
	var is_ok = true;
	var seqb_exists = document.getElementById('seqb');

	for (field in required_fields) {
		if (document.getElementById(field) && document.getElementById('status_'+field)) {
			if (field == 'email') { // fixme... should be another logic
				if (fn_check_email(document.getElementById(field).value, email_invalid) == false) {
					document.getElementById('status_'+field).innerHTML = warning_mark;
					is_ok = false;
					continue;
				}
			}
			if (required_fields[field] == 'Y') {
				document.getElementById('status_'+field).innerHTML = (document.getElementById(field).value == "")? warning_mark : "&nbsp;";
				if (seqb_exists && document.getElementById('seqb').checked == true && field.substr(0,2)=='s_') {
					continue;
				}

				is_ok = (document.getElementById(field).value == "")?false:(is_ok==true)?true:false;
			} else {
				document.getElementById('status_'+field).innerHTML = '&nbsp;';
			}
		}
	}
	if (is_ok == false) {
		alert(message);
	}
	return is_ok;
}

function fn_copy_fields(status)
{
	if (status == true) {
		if (document.getElementById('s_firstname')&&document.getElementById('b_firstname')) {
			document.getElementById('s_firstname').value = document.getElementById('b_firstname').value;
		}
		if (document.getElementById('s_lastname')&&document.getElementById('b_lastname')) {
			document.getElementById('s_lastname').value = document.getElementById('b_lastname').value;
		}
		if (document.getElementById('s_title')&&document.getElementById('b_title')) {
			document.getElementById('s_title').value = document.getElementById('b_title').value;
		}
		if (document.getElementById('s_address')&&document.getElementById('b_address')) {
			document.getElementById('s_address').value = document.getElementById('b_address').value;
		}
		if (document.getElementById('s_address_2')&&document.getElementById('b_address_2')) {
			document.getElementById('s_address_2').value = document.getElementById('b_address_2').value;
		}
		if (document.getElementById('s_city')&&document.getElementById('b_city')) {
			document.getElementById('s_city').value = document.getElementById('b_city').value;
		}
		if (document.getElementById('s_state')&&document.getElementById('b_state')) {
			document.getElementById('s_state').value = document.getElementById('b_state').value;
		}
		if (document.getElementById('s_country')&&document.getElementById('b_country')) {
			document.getElementById('s_country').value = document.getElementById('b_country').value;
		}
		if (document.getElementById('s_zipcode')&&document.getElementById('b_zipcode')) {
			document.getElementById('s_zipcode').value = document.getElementById('b_zipcode').value;
		}
	}
}

function fn_copy_field(id)
{
	if (document.getElementById('s_'+id)) {
		document.getElementById('s_'+id).value = document.getElementById('b_'+id).value;
	}
}

function fn_switch_required_field(elm, status)
{

	if (status == 'required') {
		if (required_fields[elm] == 'N') {
			document.getElementById('mark_'+elm).innerHTML = "*";
			required_fields[elm] = 'Y';
		}
	} else {
		if (required_fields[elm] == 'Y') {
			document.getElementById('mark_'+elm).innerHTML = "";
			required_fields[elm] = 'N';
		}
	}
}

function fn_rebuild_states(country_code, elm, selected_state, rebuild_shipping)
{
	sbox = document.getElementById(elm);
	var i = 0;
	if (sbox) {
		sbox.options.length = 1;
	
		if (states[country_code]) { // Populate selectbox with states
			for (k in states[country_code]) {
				i++;
				sbox.options[i] = new Option(states[country_code][k],k);
				if (k == selected_state) {
					sbox.options[i].selected = true;
				}
			}
			sbox.disabled = false;
			fn_switch_required_field(elm, 'required');
		} else { // Disable states
			sbox.disabled = true;
			fn_switch_required_field(elm, 'non-required');

		}
	}
}

function enable_dealer(myForm){
	
	var mform=document.getElementById(myForm);
	if (mform.dealer.checked == true){
			mform.assignableDealer.disabled = false;
			mform.assignableEndUser.disabled = true;
			mform.endUser.checked = false;
		}
}
function enable_endUser(myForm){
	
	var mform=document.getElementById(myForm);
	if (mform.endUser.checked == true){
		mform.assignableDealer.disabled = true;
		mform.assignableEndUser.disabled = false;
		mform.dealer.checked = false;
	}
}

function disable_dealer_endUser(myForm){
	
	var mform=document.getElementById(myForm);
	mform.assignableDealer.disabled = true;
	mform.assignableEndUser.disabled = true;

}


//----------------------------------------------validari------------------------------------

function fn_check_email(email, msg) {
	
	var filter=/^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i;
	if (filter.test(email)) {
		return true;
	} else{
		alert(msg);
		return false;
	}
}



function fn_check_required(myForm) {
	
	var mform=document.getElementById(myForm);
	for (field in required_fields) {
		if (field == "email" || field == "conemail"){
			return fn_check_email(eval("mform."+field+".value") , "You must supply a valid E-mail.");
		}else{
			if (eval("mform."+field+".value") == ""){
				alert(required_fields[field]+" required");
				return false;
			}
		}
	}
	return true;
}

function check_email(myForm) {
	
	var mform=document.getElementById(myForm);
	for (field in required_fields) {
		if (field == "email" || field == "conemail" || field== "recipient_email"){
			return fn_check_email(eval("mform."+field+".value") , "You must supply a valid E-mail.");
		}
	}
	return true;
}

function fn_check_cat(myForm1){
	//alert("cucu");	
	
	var myForm=document.getElementById(myForm1);
	var hasSelections = false;
	
	for(var i = 0; i < myForm.elements['category[]'].length; i++)
		{
		  if(myForm.elements['category[]'].options[i].selected){
		  	
		  	if (myForm.elements['category[]'].options[i].value != ""){
		  		 hasSelections = true;
		  		 break;
		  	}
		  }
		}
	if(!hasSelections)
		{
		   alert("No Category Selected");
		   return false;
		}
	
	return true;
}

function fn_check_url(myForm,fieldUrl) { 
	
  	var mform=document.getElementById(myForm);
    var filter=/^(http:\/\/www.|https:\/\/www.|ftp:\/\/www.){1}([\w]+)(.[\w]+){1,2}$/;
    
    if (!filter.test(eval("mform."+fieldUrl+".value"))) { 
        alert("You must supply a valid URL."); 
        return false; 
    }
    
    return true;
} 

function fn_check_pass(myForm){
	
	var mform=document.getElementById(myForm);
	
	if ( mform.password1.value.length < 3 ) {
		alert(" Passsword must have at least 4 characters ");
		return false;
	}else{
		if (mform.password1.value == mform.password2.value) {
			return true;
		}else{
			alert(" Password not confirmed ");
			return false;
		}
	}
}



function check_4_addAcc(myForm){
	
	if (fn_check_required(myForm)){
		if (fn_check_pass(myForm)){
			var mform=document.getElementById(myForm);
			if(mform.email.value != ""){
				if(fn_check_email(mform.email.value,"You must supply a valid e-mail address") == false)
					return false;
			}
			if (mform.url.value != ""){
				return fn_check_url(myForm,"url");
			}else{	
				return true;
			}
		}
	}
	return false;
}


function hideShowForm() {

    var x = document.getElementById("showForm");

    x.style.visibility = "hidden";
    x.style.display = "none";
    // x.innerHTML = "";

    var y = document.getElementById("addadformTable");
    
    y.style.display = "";
    y.style.visibility = "visible";

}

function check_4_addAds(myForm){

	if (fn_check_required(myForm)){
		if (fn_check_cat(myForm)){
		var mform=document.getElementById(myForm);
			if(mform.email.value != ""){
				if(fn_check_email(mform.email.value,"You must supply a valid e-mail address") == false)
					return false;
			}
			if (mform.urls.value != ""){
				return fn_check_url(myForm,"urls");
			}else{
				return true;
			}
		}
	}
	return false;
}

/////la bannere
function check_4_addBanners(myForm){
	
		if (fn_check_required(myForm)){
			if (fn_check_cat(myForm)){
				var mform=document.getElementById(myForm);
				if (mform.bannerUrl.value != ""){
					return fn_check_url(myForm,"bannerUrl");
				}else{
					return true;
				}
			}
		}
	
	return false;
}

function check_4_modifyBanners(myForm){
	
		
			if (fn_check_cat(myForm)){
				var mform=document.getElementById(myForm);
				if (mform.bannerUrl.value != ""){
					return fn_check_url(myForm,"bannerUrl");
				}else{
					return true;
				}
			}
		
	
	return false;
}

function check_4_modifyAds(myForm){
	
	if (fn_check_required(myForm)){
		if (fn_check_cat(myForm)){
			var mform=document.getElementById(myForm);
			if(mform.conemail.value != ""){
					if(fn_check_email(mform.conemail.value,"You must supply a valid e-mail address") == false)
						return false;
			}
			if (mform.conurl.value != ""){
				return fn_check_url(myForm,"conurl");
			}else{
				return true;
			}
		}
	}
	return false;
}


function check_4_addJoin(myForm){
	
	if (fn_check_required(myForm)){
		
				return true;
		
	}
	return false;
}

function check_4_addAd(myForm){

	return fn_check_cat(myForm);	
}

function check_4_modifyAd(myForm){

	if (fn_check_required(myForm)){
		var mform=document.getElementById(myForm);
		if (mform.conurl.value != ""){
				if (fn_check_url(myForm,"conurl")){
					return fn_check_cat(myForm);
				}
			}else{
				return fn_check_cat(myForm);
			}
	}
	return false;
}

function check_4_notify(myForm1){
	
	var myForm=document.getElementById(myForm1);
	//var hasSelections = false;
	
	for(var i = 0; i < myForm.elements['ucategory[]'].length; i++)
		{
		  if(myForm.elements['ucategory[]'].options[i].selected)
		  {

		  	if (myForm.elements['ucategory[]'].opTions[i].value != "") {
		  		 // hasSelections = true;
		  		 // break;
				alert("ucategory");
				return true;
		  	}
		  }
		}
	for(var i = 0; i < myForm.elements['ncategory[]'].length; i++)
		{
		  if(myForm.elements['ncategory[]'].options[i].selected)
		  {
		  	if (myForm.elements['ncategory[]'].options[i].value != ""){
		  		// hasSelections = true;
		  		// break;
				alert("ncategory");
				return true;
		  	}
		  }
		}
	for(var i = 0; i < myForm.elements['scategory[]'].length; i++)
		{
		  if(myForm.elements['scategory[]'].options[i].selected)
		  {
		  	if (myForm.elements['scategory[]'].options[i].value != ""){
		  		// hasSelections = true;
		  		// break;
				alert("scategory");
				return true;		
		  	}
		  }
		}
	for(var i = 0; i < myForm.elements['wcategory[]'].length; i++)
		{
		  if(myForm.elements['wcategory[]'].options[i].selected)
		  {
		  	if (myForm.elements['wcategory[]'].options[i].value != ""){
		  		 // hasSelections = true;
		  		 // break;
				alert("wcategory");
		  		return true;
			}
		  }
		}/*
		if(!hasSelections)
		{
		   alert("No category Selected");
		   return false;
		}
	
	return true;
     */
	alert("No category Selected");
	return false;
}

function checkNumber(e) {

    var keyCode = (e.keyCode != 0) ? e.keyCode : e.charCode;

    if(keyCode != 8 && (keyCode < 47 || keyCode > 57)) {

	alert("please enter amount only");
	return false;

    }

    return true;

}
