﻿function o(para){ return document.getElementById(para);}

var form1 = o("output_data_staff");
//var form2 = o("output_data_software");
//var form3 = o("output_data_hardware");
var form4 = o("output_data_joboverview");

var add1 = o("add_staff");
//var add2 = o("add_software");
//var add3 = o("add_hardware");
var add4 = o("add_joboverview");

var clr1 = o("delete_staff");
//var clr2 = o("delete_software");
//var clr3 = o("delete_hardware");
var clr4 = o("delete_joboverview");

var staff_quantity = o("q_staff");
var staff_title    = o("d_staff");

//var software_quantity = o("q_software");
//var hardware_quantity = o("q_hardware");
var job_overview_count = 0;

setTimeout("initiateDynamicForm()",2000);

function set_object_loc(s1,s2){
   var o1  = o(s1);
   var o2  = o(s2);
   
   o2.style.display = "block";

   o2.style.left = parseInt(o1.offsetLeft) + "px";
   o2.style.width = parseInt(o1.offsetWidth) + "px";
   o2.style.top  = parseInt(o1.offsetTop + o1.offsetHeight) + "px";
}
   
function filter_post(){
   //var required_fields = "company_name::representative_name::rep_email::office_tel::validation_id::description::q_staff::d_staff::d_joboverview".split("::");
   // irvin : 20100202 : changes had to be done for the client names 
   var required_fields = "representative_name::email::rep_mobile_num::company_name::coy_address::validation_id::description::q_staff::d_staff::d_joboverview".split("::");
   var loops = required_fields.length;
   var errors = 0;
   var error_text = "";

   //var cref_items       = "client,authorized_reseller,employee,magazine,tradeshow,online_directory,othersource".split(",");
   //var cref_items_loops = cref_items.length;

   var cref       = document.getElementsByTagName("input");
   //var cloops     = cref.length;
   //var cref_count = 0;
   //var cref_total = 8;
  
   //var rep_aim = document.getElementById("rep_aim").value;
   //var rep_icq =  document.getElementById("rep_icq").value;
   //var rep_skype = document.getElementById("rep_skype").value;	
   
  
   
  

   for(i = 0; i < loops; i++){
      var obj = o(required_fields[i].toString());
      if(obj.value.length <= 0) errors++;
	 
      try{
         if(i == 2 && obj.value.length > 0){
			 // alert(obj.value);
            if(obj.value.split("@").length <= 1){
               errors++;
               error_text += "Invalid Email format.\r\n";
            }
            if(obj.value.split("@")[0].length <= 0){
               errors++;
               error_text += "Invalid Email format.\r\n";
            }
            if(obj.value.split("@")[1].split(".").length <= 1){
               errors++;
               error_text += "Invalid Email format.\r\n";
            }
            if(obj.value.split("@")[1].split(".")[0].length <= 0){
               errors++;
               error_text += "Invalid Email format.\r\n";
            }
            if(obj.value.split("@")[1].split(".")[1].length <= 0){
               errors++;
               error_text += "Invalid Email format.\r\n";
            }
         }
      } catch(err){}
   }
   
   /*for(i = 0; i < cloops; i++){
      if(cref[i].getAttribute("type").toLowerCase == "radio"){
         if(cref[i].checked == false){
            cref_count++;			
            error_text += "Invalid Email format.\r\n";
         } 
      }
   } */
   
   /*  if ((rep_aim <= '') && (rep_icq   <= '') && (rep_skype  <= '')) {
	   	alert('Please encode either skype, aim or icq details!!');
		return false;	
     }//if 
	 */
      
   //if(cref_count == cref_total) errors++;

   if(errors <= 0){
      return true;
   } else {
      error_text += "Please fill up [" + errors + "] of the required field(*).";
      alert(error_text);
      return false;
   }
   
   
   
  
   
}

function add_data_entry(p1,d1,d2,n1,n2){
   if( d1.length <= 0 || parseInt(d1).toString() == 'NaN' || d2.length <= 0 ) return;
   var div1 = document.createElement("div");
   var in1  = document.createElement("input");
   var in2  = document.createElement("input");
   in1.setAttribute("name",n1+"[]");
   in1.setAttribute("type","textbox");
   in1.setAttribute("value",parseInt(d1));
   in1.style.width = "40px";
   in1.style.border = "0px";
   in1.style.bgColor = "none";
   in1.className = "data_input";

   in2.setAttribute("name",n2+"[]");
   in2.setAttribute("type","textbox");
   in2.setAttribute("value",d2);
   in2.style.width = "300px";
   in2.style.border = "0px";
   in2.style.bgColor = "none";
   in2.className = "data_input";
   
   in1.onchange = function(){
      if( this.value.length <= 0 || parseInt(this.value).toString() == 'NaN') 
         this.value = "1";
      else
         this.value = parseInt(this.value);
   }
   
   div1.appendChild(in1);
   div1.appendChild(in2);
   p1.appendChild(div1);
}

function re_check(obj,obj_name){
   var obj_src = obj;
   var obj_des = o(obj_name);
   if(obj_src.checked == false){
      obj_des.value = '';
      obj_des.onblur = null;
   } else {
      obj_des.focus();
      obj_des.onblur = function(){
         if(this.value <= 3){
            alert("Please enter atleast 4 character");
            this.focus();
         }
      };
   }
}

function initiateDynamicForm(){
   add1.onclick = function(){
      add_data_entry(form1,o("q_staff").value,o("d_staff").value,"staff_quantity","staff_title");
      o("staff_selection").style.display    = "none";
      o("quantity_selection").style.display = "none"; };
   //add2.onclick = function(){
   //   add_data_entry(form2,o("q_software").value,o("d_software").value,"software_quantity","software_type");
   //   o("software_quantity").style.display = "none"; };
   //add3.onclick = function(){
   //   add_data_entry(form3,o("q_hardware").value,o("d_hardware").value,"hardware_quantity","hardware_type");
   //  o("hardware_quantity").style.display = "none"; };
   add4.onclick = function(){job_overview_count++;
   add_data_entry(form4,job_overview_count,o("d_joboverview").value,"joboverview_quantity","joboverview_type"); };

   clr1.onclick = function() { form1.innerHTML = "";  };
   //clr2.onclick = function() { form2.innerHTML = "";  };
   //clr3.onclick = function() { form3.innerHTML = "";  };
   clr4.onclick = function() { form4.innerHTML = "";  job_overview_count = 0; };

   staff_title.onfocus    = function() { set_object_loc("d_staff","staff_selection"); };
   staff_title.onclick    = function() { set_object_loc("d_staff","staff_selection"); };

   
   staff_quantity.onfocus = function() { set_object_loc("q_staff","quantity_selection"); };
   staff_quantity.onclick = function() { set_object_loc("q_staff","quantity_selection"); };

   //software_quantity.onfocus = function() { set_object_loc("q_software","software_quantity"); };
   //software_quantity.onclick = function() { set_object_loc("q_software","software_quantity"); };

   //hardware_quantity.onfocus = function() { set_object_loc("q_hardware","hardware_quantity"); };
   //hardware_quantity.onclick = function() { set_object_loc("q_hardware","hardware_quantity"); };

   o("quantity_selection").onchange = function(){ this.style.display = "none"; o("q_staff").value = this.value; o("staff_selection").style.display    = "none"; };
   o("staff_selection").onchange    = function(){ this.style.display = "none"; o("d_staff").value = this.value; o("quantity_selection").style.display = "none"; };

   //o("software_quantity").onchange    = function(){ this.style.display = "none"; o("q_software").value    = this.value; };
   //o("hardware_quantity").onchange    = function(){ this.style.display = "none"; o("q_hardware").value    = this.value; };
}


// irvin: 20100128: Validation Scripts Start here
function validate_required(field,alerttxt) {
	with (field) {
		if (value==null||value=="") {
			alert(alerttxt);
			return false;
		} else {
			return true;
		}
	}
}

function validate_email(field,alerttxt){
	with (field){
		apos=value.indexOf("@");
		dotpos=value.lastIndexOf(".");
		if (apos<1||dotpos-apos<2) {
			alert(alerttxt);
			return false;
		} else {
			return true;
		}
	}
}

function lengthRestriction(field,alerttxt){
	with (field){
		var min = 7;
		var max = 11;
		var uInput = field.value;
		if(uInput.length >= min && uInput.length <= max){
			return true;
		}else{
			alert(alerttxt);
			return false;
		}
	}
}

function isNumeric(field, alerttxt){
	with (field){
		var numericExpression = /^[0-9]+$/;
		if(field.value.match(numericExpression)){
				return true;
		} else {
			alert(alerttxt);
			return false;
		}
	}
}

function checkradio(field, alerttxt) {
	with (field) {
	if (!document.form.referred_shift.checked && !document.form.referred_shift.checked ) {
		alert ('Please choose between AM/PM shift.');
		return false;
		} else return true;	
	}
}

function validate_form(thisform) {
	with (thisform){
		if (validate_required(representative_name,"First name must be filled out!")==false) {
			representative_name.focus();
			return false;
		}
		if (validate_required(rep_email,"Email must be filled out!")==false) {
			rep_email.focus();
			return false;
		}
		if (validate_email(rep_email,"Not a valid e-mail address!")==false) {
			rep_email.focus();
			return false;
		}	
		if (validate_required(rep_mobile_num,"Phone must be filled out!")==false) {
			rep_mobile_num.focus();
			return false;
		} 
		if (lengthRestriction(rep_mobile_num,"Please enter between 7 and 11 numbers")==false) {
			rep_mobile_num.focus();
			return false;
		}		
		if (isNumeric(rep_mobile_num,"Not a valid phone number!")==false) {
			rep_mobile_num.focus();
			return false;						
		}
		if (validate_required(company_name,"Company name must be filled out!")==false) {
			company_name.focus();
			return false;
		}			
		if (validate_required(coy_address,"Company address must be filled out!")==false) {
			coy_address.focus();
			return false;
		}			
		if (validate_required(q_staff,"Quantity needs to be filled out!")==false) {
			q_staff.focus();
			return false;
		}	
		if (validate_required(d_staff,"No Staff!")==false) {
			d_staff.focus();
			return false;
		}		
		if (validate_required(d_joboverview,"No Job Overview!")==false) {
			d_joboverview.focus();
			return false;
		}	
		if (validate_required(representative_src,"Information is required!")==false) {
			representative_src.focus();
			return false;
		}	
		if (validate_required(validation_id,"Information is required!")==false) {
			validation_id.focus();
			return false;
		}			
	}
}

// irvin: 20100128 : end validation script
