jQuery(document).ready(function(){
	
	// for home page quote form
    jQuery("#hompage_quote_form").submit(function(){
        if(validateQuoteForm())
        {
            startLoading();
            jQuery.post("ajax/submitQuote.php",jQuery("#hompage_quote_form").serialize(),updateMessage);            
        }
        return false;
    });
	
	// package order form
    jQuery("#std_pack_order_form").submit(function(){
         if(validateOrderForm())
        {
            return true;
        }
        else
        {
            return false;
        }
    });
	
	// psd to html order form
	jQuery("#psd-to-html-form").submit(function(){												
		return validatePsdToHtmlForm();		
    });
	
	// psd to wp order form
	jQuery("#psd-to-wp-form").submit(function(){
        return validatePsdToWPForm();
    });
	
	// for order form sliding tabs
	jQuery(".options_body").hide();
	jQuery(".first-options-title").addClass('expanded'); // to set first panel title background
	jQuery(".first-options-panel").show(); // to show first panel background
	
	jQuery(".options_heading").click(function(){
		jQuery(this).toggleClass('expanded');
		jQuery(this).next(".options_body").slideToggle('slow');
	 });
  
	// auto grow text area
	jQuery('textarea[name=description]').autoGrow();
	jQuery('textarea[name=notes]').autoGrow();
	jQuery('textarea[name=paymentFor]').autoGrow();
	jQuery('textarea[name=comment]').autoGrow();
	jQuery('textarea[name=message]').autoGrow();

	// for validation purposes : this function attached focus and blur events with input elements
	var addFocusAndBlur = function($input, $val){
			
			$input.focus(function(){
				if (this.value == $val) {this.value = '';}
			});
			
			$input.blur(function(){
				if (this.value == '') {this.value = $val;}
			});
	}
	
	addFocusAndBlur(jQuery('#name'),'Name');
	addFocusAndBlur(jQuery('#email'),'Email');
	addFocusAndBlur(jQuery('#notes'),'Notes');
	addFocusAndBlur(jQuery('#link'),'Link');

});

function updateMessage(data){
    stopLoading();
    jQuery("#reply").html(data).show();
    resetQuoteForm();
}

function updateMessageInOrderForm(data){
    stopLoading();
	if(data == 'True')
	{
    	jQuery('.order-form-area').fadeOut('slow',function(){
										jQuery('.order-placed-successfully').fadeIn('slow');									
									});
	}
	else
	{
		jQuery("#reply").html("Failed to place order! Use contact form as alternative.").show();
	}
}

function startLoading(){
    jQuery("#reply").hide();
    jQuery("#loading").fadeIn("slow");
}

function stopLoading(){
    jQuery("#loading").hide();
}

function resetQuoteForm(){
    jQuery("#name").val("Name");
    jQuery("#email").val("Email");
    jQuery("#description").val("Description");
    jQuery("#link").val("Link");
}

function isValidEmailAddress(emailAddress) {
    var pattern = new RegExp(/^(("[\w-\s]+")|([\w-]+(?:\.[\w-]+)*)|("[\w-\s]+")([\w-]+(?:\.[\w-]+)*))(@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$)|(@\[?((25[0-5]\.|2[0-4][0-9]\.|1[0-9]{2}\.|[0-9]{1,2}\.))((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){2}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\]?$)/i);
    return pattern.test(emailAddress);
}

function validateQuoteForm(){
    // validate name
    if(jQuery("#name").val().length == 0 || jQuery("#name").val() == "Name")
    {
        jQuery("#name").addClass("error");

        return false;
    }else{
        jQuery("#name").removeClass("error");
    }
    // validate email
    if(!isValidEmailAddress(jQuery("#email").val()))
    {
        jQuery("#email").addClass("error");
        return false;
    }else{
        jQuery("#email").removeClass("error");
    }
    // validate project description
    if(jQuery("#description").val().length == 0 || jQuery("#description").val() == "Description")
    {
        jQuery("#description").addClass("error");
        return false;
    }else{
        jQuery("#description").removeClass("error");
    }

    return true;
}

function validateOrderForm(){
    // validate name
    if(jQuery("#name").val().length == 0 || jQuery("#name").val() == "Name")
    {
        jQuery("#name").addClass("error");
        return false;
    }else{
        jQuery("#name").removeClass("error");
    }
    // validate email
    if(!isValidEmailAddress(jQuery("#email").val()))
    {
        jQuery("#email").addClass("error");
        return false;
    }else{
        jQuery("#email").removeClass("error");
    }
    // validate project description
    if(jQuery("#description").val().length == 0 || jQuery("#description").val() == "Description...")
    {
        jQuery("#description").addClass("error");
        return false;
    }else{
        jQuery("#description").removeClass("error");
    }

    return true;
}

// validate psd to html form
function validatePsdToHtmlForm(){
    // validate name
    if(jQuery("#name").val().length == 0 || jQuery("#name").val() == "Name")
    {
        jQuery("#name").addClass("error").focus();
        return false;
    }else{
        jQuery("#name").removeClass("error");
    }
	
    // validate email
    if(!isValidEmailAddress(jQuery("#email").val()))
    {
        jQuery("#email").addClass("error").focus();
        return false;
    }else{
        jQuery("#email").removeClass("error");
    }
	
    // validate project description
    if(jQuery("#notes").val().length == 0 || jQuery("#notes").val() == "Notes")
    {
        jQuery("#notes").addClass("error").focus();;
        return false;
    }else{
        jQuery("#notes").removeClass("error");
    }

    return true;
}

// validate psd to html form
function validatePsdToWPForm(){
    // validate name
    if(jQuery("#name").val().length == 0 || jQuery("#name").val() == "Name")
    {
        jQuery("#name").addClass("error").focus();
        return false;
    }else{
        jQuery("#name").removeClass("error");
    }
	
    // validate email
    if(!isValidEmailAddress(jQuery("#email").val()))
    {
        jQuery("#email").addClass("error").focus();
        return false;
    }else{
        jQuery("#email").removeClass("error");
    }
	
    // validate project description
    if(jQuery("#notes").val().length == 0 || jQuery("#notes").val() == "Notes")
    {
        jQuery("#notes").addClass("error").focus();;
        return false;
    }else{
        jQuery("#notes").removeClass("error");
    }

    return true;
}


// PSD to HTML Order Form
function updatePSDtoHTMLOrderForm()
{
	var packagePrice = 40;

    var pages = parseInt(document.getElementById("pages").value);
    document.getElementById("numberOfPages").innerHTML = pages;

    var rate = 25;
	
    var subPagesPrice = 0;
        if( pages > 1)
        {
            subPagesPrice = (pages-1) * rate;
        }

    var subTotal = packagePrice + subPagesPrice;
    document.getElementById("subTotal").innerHTML = subTotal;

    var layoutWidthPrice = getLayoutWidthPrice();
	var nonWebFontsPrice = getNonWebFontsPrice();
    var dropDownMenusPrice = getDropDownMenusPrice();
	var markupTypePrice = getMarkupTypePrice();
	
	// print version css price
	var printCssPrice = 0;	if(document.getElementById("printVersionCSS").checked == true){	printCssPrice = 10; }
	
	// commented markup price
	var commentedMarkupPrice = 0;	if(document.getElementById("commentedMarkup").checked == true){ commentedMarkupPrice = 5; }
	
	// accordion price
	var accordionPrice = 0;	if(document.getElementById("accordion").checked == true){ accordionPrice = 10; }
	
	// tabs price
	var tabsPrice = 0;	if(document.getElementById("tabs").checked == true){ tabsPrice = 10; }
	
	// slider price
	var sliderPrice = 0; if(document.getElementById("slider").checked == true) { sliderPrice = 10; }
	
	// lightBox price
	var lightBoxPrice = 0;	if(document.getElementById("lightBox").checked == true) { lightBoxPrice = 10; }
	
    var extraOptionsPrice = layoutWidthPrice + nonWebFontsPrice + dropDownMenusPrice + markupTypePrice + printCssPrice + commentedMarkupPrice + accordionPrice + tabsPrice + sliderPrice + lightBoxPrice ;
    document.getElementById("extraOptions").innerHTML = extraOptionsPrice;

    var totalPrice = subTotal + extraOptionsPrice;
    document.getElementById("totalPrice").innerHTML = totalPrice;
	document.getElementById("total").value = totalPrice;
}

// PSD to HTML Order Form
function updatePSDtoWPOrderForm()
{
	var packagePrice = 90;

    var pages = parseInt(document.getElementById("pages").value);
    document.getElementById("numberOfPages").innerHTML = pages;

    var rate = 35;
	
    var subPagesPrice = 0;
        if( pages > 1)
        {
            subPagesPrice = (pages-1) * rate;
        }

    var subTotal = packagePrice + subPagesPrice;
    document.getElementById("subTotal").innerHTML = subTotal;
	
    var layoutWidthPrice = getLayoutWidthPrice();
	var nonWebFontsPrice = getNonWebFontsPrice();
    var dropDownMenusPrice = getDropDownMenusPrice();
	var markupTypePrice = getMarkupTypePrice();
	
	// install theme price
	var installThemePrice = 0;	if(document.getElementById("intallTheme").checked == true){	installThemePrice = 0; }
	
	// print version css price
	var printCssPrice = 0;	if(document.getElementById("printVersionCSS").checked == true){	printCssPrice = 10; }
	
	// commented markup price
	var commentedMarkupPrice = 0;	if(document.getElementById("commentedMarkup").checked == true){ commentedMarkupPrice = 5; }
	
	// accordion price
	var accordionPrice = 0;	if(document.getElementById("accordion").checked == true){ accordionPrice = 10; }
	
	// tabs price
	var tabsPrice = 0;	if(document.getElementById("tabs").checked == true){ tabsPrice = 10; }
	
	// slider price
	var sliderPrice = 0; if(document.getElementById("slider").checked == true) { sliderPrice = 10; }
	
	// lightBox price
	var lightBoxPrice = 0;	if(document.getElementById("lightBox").checked == true) { lightBoxPrice = 10; }
	
    var extraOptionsPrice = installThemePrice + layoutWidthPrice + nonWebFontsPrice + dropDownMenusPrice + markupTypePrice + printCssPrice + commentedMarkupPrice + accordionPrice + tabsPrice + sliderPrice + lightBoxPrice ;
    document.getElementById("extraOptions").innerHTML = extraOptionsPrice;

    var totalPrice = subTotal + extraOptionsPrice;
    document.getElementById("totalPrice").innerHTML = totalPrice;
	document.getElementById("total").value = totalPrice;
}

// get checked value
function getCheckedValue(radioObj)
{
    if(!radioObj)
        return "";

    var radioLength = radioObj.length;

    if(radioLength == undefined)
        if(radioObj.checked)
                return radioObj.value;
        else
                return 0;

    for(var i = 0; i < radioLength; i++)
    {
        if(radioObj[i].checked)
        {
                return radioObj[i].value;
        }
    }
    return 0;
}

// get layout width price
function getLayoutWidthPrice()
{
    var layoutWidth = getCheckedValue(document.getElementsByName("layoutWidth"));
    var layoutWidthPrice = 0;

     switch (layoutWidth)
    {
        case "fixed":
            layoutWidthPrice = 0;
            break;
        case "liquid":
            layoutWidthPrice = 10;
            break;
        default:
            layoutWidthPrice = 0;
            break;
    }

    return layoutWidthPrice;
}

// get dropdownmenu price
function getDropDownMenusPrice()
{
    var dropDownMenus = getCheckedValue(document.getElementsByName("dropDownMenus"));
    var dropDownMenusPrice = 0;

    switch (dropDownMenus)
    {
        case "no":
            dropDownMenusPrice = 0;
            break;
        case "One Level":
            dropDownMenusPrice = 10;
            break;
        case "Multi Level":
            dropDownMenusPrice = 15;
            break;
        default:
            dropDownMenusPrice = 0;
            break;
    }

    return dropDownMenusPrice;
}

// get markupt price
function getMarkupTypePrice()
{
    var markupType = getCheckedValue(document.getElementsByName("markupType"));
    var markupTypePrice = 0;

    switch (markupType)
    {
        case "Transitional":
            markupTypePrice = 0;
            break;
        case "Strict":
            markupTypePrice = 0;
            break;
        case "HTML5":
            markupTypePrice = 10;
            break;
        default:
            markupTypePrice = 0;
            break;
    }

    return markupTypePrice;
}

// get nonWebFonts Choice Price
function getNonWebFontsPrice()
{
    var nonWebFontsChoice = getCheckedValue(document.getElementsByName("nonWebFonts"));
    var nonWebFontsPrice = 0;

    switch (nonWebFontsChoice)
    {
        case "Image Replacement":
            nonWebFontsPrice = 0;
            break;
        case "@font-face":
            nonWebFontsPrice = 0;
            break;
        case "Cufon":
            nonWebFontsPrice = 10;
            break;
		case "Sifr":
			nonWebFontsPrice = 15;
            break;
        default:
            nonWebFontsPrice = 0;
            break;
    }

    return nonWebFontsPrice;
}
